﻿<?php
include_once("./function.php");
//print_r($_POST);die;//Array ( [pname] => SSC [id] => 24 [addr] => 杭州 [price] => 999 [yh] => 111 [keywords] => gjz [bh] => 001 [kc] => 1000 [sj] => 0 [color] => Array ( [0] => #0000a0 ) [size] => Array ( [0] => XS [1] => M ) [introduce] => SSS [sub] => 添加 )
//print_r($_FILES);die;//Array ( [bimg] => Array ( [name] => 2.jpg [type] => image/jpeg [tmp_name] => C:\Windows\Temp\php9897.tmp [error] => 0 [size] => 19041 ) [simg] => Array ( [name] => Array ( [0] => ) [type] => Array ( [0] => ) [tmp_name] => Array ( [0] => ) [error] => Array ( [0] => 4 ) [size] => Array ( [0] => 0 ) ) )
extract($_POST);//$price $yh
extract($_FILES);
$sj=trim($sj);
$size=implode(',',$size);
//var_dump($size);die;
$color=implode(',',$color);
$bimg=implode(',',$bimg);
//$simg=implode(',',$simg);
$time=trim(time());
if(!empty($yh))
{
	$yh=trim($yh);
}
else
{
	$yh="";
}
function upload_file($name)
{
	//$name=$_FILES["bimg"]["name"];
	if(empty($name["name"]))
	{
		return false;
	}
	$dir="./uploads/";
	if(!is_dir($dir))
	{
		mkdir ($dir,0777,true);
	}
	$type=substr(strrchr($name['name'],"."),1);
	$arr=["jpg","jpeg","png","bmp","gif"];
	if(!in_array(strtolower($type),$arr))
	{
		return ("文件类型不符合");
	}

	$error=$_FILES["bimg"]["error"];
	switch($error)
	{
		case 1:
			return "文件太大";
		break;
		case 2:
			return "超过html隐藏域设置的大小";
		break;
		case 3:
			return "文件部分上传";
		break;
		case 6:
			return "找不到临时文件夹";
		break;
		case 4:
			return "没有文件被上传";
		break;
		case 7:
			return "写入临时文件夹中出错";
		break;
	}
	if($error!=0)
	{
		return ($err);
	}

	if(!is_uploaded_file($_FILES["bimg"]["tmp_name"]))
	{
	 return ("临时文件不存在");
	}

	$path=$dir.date("ymd").rand(1000,9999).".".$type;
	if(move_uploaded_file($_FILES["bimg"]["tmp_name"],$path))
	{
		return $path;
	}
	else
	{
		return false;
	}

}
$res=upload_file($_FILES["bimg"]);
//print_r($res);die;//./uploads/1901054296.jpg
if($res!='')
{//echo 1;die;
	$bimg=$res;
	//echo $bimg;die;
}
else
{
	$bimg="";
}

//Array ( [bimg] => Array ( [name] => 2.jpg [type] => image/jpeg [tmp_name] => C:\Windows\Temp\php14B2.tmp [error] => 0 [size] => 19041 ) [simg] => Array ( [name] => Array ( [0] => ) [type] => Array ( [0] => ) [tmp_name] => Array ( [0] => ) [error] => Array ( [0] => 4 ) [size] => Array ( [0] => 0 ) ) )
function get_dwj($file)
{
	if(empty(implode("",$file["name"])))
	{
		return false;
	}

	$dir="./uploads/";
	if(empty($dir))
	{
		mkdir($dir,0777,true);
	}
		$res=array();$res1="";
		for($i=0;$i<count($file["name"]);$i++)
		{
			$num=$i+1;
		if(empty($file["name"][$i]))
		{
			$res1.="第{$num}个文件没有上传";
			continue;
		}

	$type=substr(strrchr($file["name"][$i],"."),1);
	$all=["jpg","jpeg","png","bmp","gif"];
	if(!in_array($type,$all))
	{
		$res1.=("第{$num}个文件类型不符合");
	}
	switch($file["error"][$i])
	{
		case 1:
			$err="超出服务器大小";
		break;
			case 2:
				$err="超出隐藏域设置的大小";
			break;
			case 3:
				$err="部分上传";
			break;
			case 4:
				$err="没有上传";
			break;
			case 6:
				$err="找不到临时文件夹";
			break;
			case 7:
				$err="写入临时文件夹出错";
			break;
	}
	if($file["error"][$i]!=0)
	{
		$res1.= $err;
		continue;
	}
	
	if(!is_uploaded_file($file["tmp_name"][$i]))
	{
		$res1.= "临时文件夹不存在";
		continue;
	}
	$path=$dir.date("His").rand(1000,9999).".".$type;
	if(move_uploaded_file($file["tmp_name"][$i],$path))
	{
		$res[]=$path;
	}
	else
	{
		$res1.= "第{$num}文件上传失败";	
	}
   }
	if($res1!="")
	{
		
		msg($res1,"product_add.php");exit;
	}
	else
	{
		return $res;
	}

}$res=get_dwj($_FILES["simg"]);

if($res!=false)
{
	//print_r($res);die;//Array ( [0] => ./uploads/2010171367.jpg [1] => ./uploads/2010173406.jpg )
	$simg=implode(",",$res);
	//print_r($simg);die;
}
else
{
	$simg="";
}
$yh=(float)$yh;
$sql="insert into ts_product(pname,p_c_id,addr,price,sprice,keyword,pnums,snums,ifshow,color,size,thumb,pimgs,descp,pubtime) values('$pname','$id','$addr','$price','$yh','$keywords','$bh','$kc','$sj','$color','$size','$bimg','$simg','$introduce','$time')";
$res=mysqli_query($link,$sql);
if($res)
{
	if(mysqli_affected_rows($link)==1)
	{
		msg("添加成功","./product.php");exit;
	}
	else
	{
		msg("添加失败","./product_add.php");exit;
	}

}
else
{
	echo "错误码:".mysqli_errno($link)."错误信息:".mysqli_error($link);exit;
}

?>